<template>
    <div class="table-page">
        <div class="table-operation-box">
            <el-button
                type="primary"
                plain
                style="margin-bottom: 2em"
                icon="el-icon-plus"
                @click="clickAdd('添加行业')"
                >新增数据
            </el-button>
        </div>
        <div class="table-data-box">
            <el-table
                ref="table"
                v-loading="tableLoading"
                :data="tableData"
                border
                style="width: 100%;min-height:40em"
                row-key="id"
            >
                <el-table-column
                    prop="id"
                    label="行业值"
                    width="200"
                ></el-table-column>
                <el-table-column label="图标" width="120" align="center">
                    <template slot-scope="scope">
                        <img
                            v-if="scope.row.icon"
                            :src="coverImage(scope.row.icon)"
                            class="icon"
                        />
                        <p v-else>无</p>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="行业名称(前台)"
                    min-width="100"
                >
                </el-table-column>
                <!-- <el-table-column
                    prop="serverName"
                    label="行业别名(后台显示)"
                    min-width="100"
                >
                </el-table-column> -->
                <el-table-column
                    prop="serverName"
                    label="行业名称（服务端）"
                    min-width="100"
                >
                </el-table-column>
                <el-table-column prop="sort" label="排序" width="150">
                    <template
                        slot-scope="scope"
                        v-loading="scope.row.opLoading"
                    >
                        <div
                            v-show="!scope.row.sortEdit"
                            @dblclick="scope.row.sortEdit = true"
                        >
                            {{ scope.row.sort }}
                        </div>
                        <el-input
                            v-show="scope.row.sortEdit"
                            v-model="scope.row.sort"
                            @blur="
                                sortEditSave(
                                    '/exam/industry/change-sort',
                                    scope.row
                                )
                            "
                        ></el-input>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="examCount"
                    label="考试数量"
                    width="120"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    width="200"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            v-if="!scope.row.edit"
                            class="primary"
                            type="text"
                            size="mini"
                            icon="el-icon-edit"
                            :loading="scope.row.opLoading"
                            @click="
                                clickSimpleEdit(scope.row, '数据编辑', () => {})
                            "
                            >编辑
                        </el-button>
                        <el-button
                            v-if="!scope.row.edit"
                            class="warning"
                            type="text"
                            icon="el-icon-delete"
                            size="mini"
                            :loading="scope.row.opLoading"
                            @click="
                                tableDelete(
                                    '/exam/industry/delete',
                                    {
                                        id: scope.row.id
                                    },
                                    true
                                )
                            "
                            >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-if="pageData.total" class="xy-between table-page-box">
            <el-pagination
                background
                :current-page.sync="pageData.currentPage"
                :page-sizes="pageData.pageSizes"
                :page-size.sync="pageData.pageSize"
                layout="total,  prev, pager, next, sizes, jumper"
                :total="pageData.total"
            >
            </el-pagination>
        </div>
        <el-dialog
            :title="dialogFormTitle"
            :visible.sync="dialogFormVisible"
            custom-class="one-dialog"
            @close="() => {
                base64Icon = null;
            }"
        >
            <el-form
                ref="dataForm"
                :model="dataForm"
                label-width="6em"
                :rules="dataFormRule"
                class="table-edit-from exam-form"
            >
                <el-form-item label="图标">
                    <el-upload
                        class="icon-uploader"
                        action="https://jsonplaceholder.typicode.com/posts/"
                        :show-file-list="false"
                        accept=".jpg,.png,.jpeg"
                        :auto-upload="false"
                        :on-change="iconChange"
                    >
                        <img
                            v-if="base64Icon || dataForm.icon"
                            :src="base64Icon || coverImage(dataForm.icon)"
                            class="icon"
                        />
                        <i v-else class="el-icon-plus icon-uploader-icon"></i>
                    </el-upload>
                </el-form-item>

                <el-form-item label="行业值" prop="id">
                    <el-input
                        v-model="dataForm.id"
                        :disabled="!dataForm.addAction"
                    ></el-input>
                </el-form-item>
                <el-form-item label="行业名称" prop="name">
                    <el-input v-model="dataForm.name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="行业别名(后台显示)" prop="serverName">
                    <el-input v-model="dataForm.serverName"></el-input>
                </el-form-item> -->
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="dataForm.sort"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button :loading="submitFlag" @click="dialogClose"
                    >取 消</el-button
                >
                <el-button
                    :loading="submitFlag"
                    type="primary"
                    @click="
                        formDataSave('/exam/industry/update', undefined, () => {
                            base64Icon = null;
                        })
                    "
                    >保 存</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>

<script>
import Fetch from "@/api/request.js";
import { TablePage } from "@/layout/mixin/iVue.js";
import { mapState } from "vuex";
import { get, put, post } from "@/api/request";
import { ossUpload, elUploadHandler } from "@/utils/file.js";

export default {
    name: "Industry",
    components: {},
    mixins: [TablePage],
    data() {
        return {
            queryData: {
                haveExamCount: true
            },
            initAddForm: {
                id: null,
                name: null,
                serverName: null,
                sort: 100,
                icon: null
            },
            base64Icon: null,
            dataFormRule: {
                id: [
                    {
                        required: true,
                        message: "请输入行业值",
                        trigger: "blur"
                    }
                ],
                name: [
                    {
                        required: true,
                        message: "请输入行业名称",
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    computed: {
        ...mapState({})
    },
    mounted() {},
    methods: {
        search(queryData) {
            this.loadTable("/exam/industry/page", queryData);
        },
        iconChange(file, fileList) {
            elUploadHandler(file, fileList, evt => {
                this.base64Icon = evt.target.result;
            }).then(result => {
                this.dataForm.icon = result;
            });
        }
    }
};
</script>

<style lang="scss" scoped>
.table-data-box {
    .icon {
        width: 60px;
        height: 60px;
        object-fit: cover;
    }
}
</style>
<style lang="css">
.exam-form .icon-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.exam-form .icon-uploader .el-upload:hover {
    border-color: #409eff;
}
.exam-form .icon-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 2.58rem;
    height: 2.58rem;
    line-height: 2.58rem;
    text-align: center;
}
.exam-form .icon {
    width: 2.58rem;
    height: 2.58rem;
    display: block;
}
</style>
